/*
 * FairyRebirthQualityResource.cpp
 *
 *  Created on: 2014. 2. 21.
 *      Author: mega
 */

#include <Toolkit/Str.h>
#include <Database/Static.h>
#include "FairyRebirthQualityResource.h"

#include "../macro.h"

namespace ProjectZ { namespace NResource {

FairyRebirthQualityResource::FairyRebirthQualityResource()
{

}

FairyRebirthQualityResource::~FairyRebirthQualityResource()
{

}

bool
FairyRebirthQualityResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_fairy_rebirth");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	clsTree.clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int quality			= 0;
		int qualityPoint	= 0;
		Toolkit::Str<30> desc;

		clsAuto->GetRecord() >> NATIONAL_CODE >> quality >> qualityPoint >> desc;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsTree[quality] = qualityPoint;
	}

	return true;
}


int
FairyRebirthQualityResource::GetRebirthQuality(int point)
{
	auto iter = clsTree.rbegin();

	for (; iter != clsTree.rend(); ++iter)
	{
		if (point >= iter->second)
			break;
	}

	return iter->first;
}

} /*NResource*/ } /*ProjectZ*/
